package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    /**
     *  查看预约是否正常
     * @param map
     * @return
     */
    @Override
    public Result submit(Map map) throws Exception {
//       1.根据预约表--查看当天有无预约设置
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(date);
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
//      2.人数已满,不可预约
//        可预约人数
        int number = orderSetting.getNumber();
//        预约人数
        int reservations = orderSetting.getReservations();
        if (reservations>=number){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
//      3.预约人员是否是会员
//          根据身份证idCard--查找t_member 是否存在数据--判断是否为会员
        String  idCard = (String) map.get("idCard");
        Member member=memberDao.findMemberByIdCard(idCard);

       if (member ==null){
           member  = new Member();
           member.setName((String)map.get("name"));
           member.setSex((String)map.get("sex"));
           member.setIdCard((String)map.get("idCard"));
           member.setPhoneNumber((String)map.get("telephone"));
           member.setRegTime(new Date());
           memberDao.addMember(member);
       }
//      4.预约名字-- 不能与身份证不一样
        if (!member.getName().equals(map.get("name"))){
           return new Result(false,MessageConstant.IDCARD_NAME_DIFFERENT);
        }
//      5.防止重复--同一个用户同一天预约同一个套餐
        Order order = new Order(member.getId(), DateUtils.parseString2Date((String)map.get("orderDate")), (String)map.get("orderType"), Integer.parseInt((String)map.get("setmealId")));
        Order or=orderDao.findOrderByCondition(order);
        if (or !=null){
            return  new Result(false,MessageConstant.HAS_ORDERED);
        }
//      6.增加预约人数
//         查找预约设置--查找预约人数
         orderSetting.setReservations(orderSetting.getReservations()+1);
         orderSettingDao.updateReservations(orderSetting);
//      7.保存预约信息
       Order o= new Order(member.getId(),DateUtils.parseString2Date((String)map.get("orderDate")),(String) map.get("orderType"),Order.ORDERSTATUS_NO,Integer.parseInt((String)map.get("setmealId")));
        orderDao.addOrder(o);
        return new Result(true,MessageConstant.ORDER_SUCCESS,o.getId());
    }

    /**
     *   根据体检id---查找相关信息--有member表，setmeal, order
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
       return orderDao.findById(id);
    }
}
